import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtPreOrder;
	private JTextField txtInOrder;
	private JTextArea textResult;
	private BiTree tree;
	private JTextField txtDepth;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("BiTree");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 519, 338);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblPreorder = new JLabel("preOrder:");
		lblPreorder.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPreorder.setBounds(14, 18, 72, 18);
		contentPane.add(lblPreorder);

		txtPreOrder = new JTextField();
		txtPreOrder.setText("ABDEGCFH");
		txtPreOrder.setBounds(92, 18, 395, 24);
		contentPane.add(txtPreOrder);
		txtPreOrder.setColumns(10);

		JLabel lblInorder = new JLabel("inOrder:");
		lblInorder.setHorizontalAlignment(SwingConstants.RIGHT);
		lblInorder.setBounds(14, 54, 72, 18);
		contentPane.add(lblInorder);

		txtInOrder = new JTextField();
		txtInOrder.setText("DBGEAFHC");
		txtInOrder.setColumns(10);
		txtInOrder.setBounds(92, 51, 395, 24);
		contentPane.add(txtInOrder);

		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnCreateCall();
			}
		});
		btnCreate.setBounds(9, 90, 113, 27);
		contentPane.add(btnCreate);

		JButton btnPreRoot = new JButton("preRoot");
		btnPreRoot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnPreRootCall();
			}
		});
		btnPreRoot.setBounds(9, 135, 113, 27);
		contentPane.add(btnPreRoot);

		JButton btnInRoot = new JButton("inRoot");
		btnInRoot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnInRootCall();
			}
		});
		btnInRoot.setBounds(131, 135, 113, 27);
		contentPane.add(btnInRoot);

		JButton btnPostRoot = new JButton("postRoot");
		btnPostRoot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnPostRootCall();
			}
		});
		btnPostRoot.setBounds(253, 135, 113, 27);
		contentPane.add(btnPostRoot);

		textResult = new JTextArea();
		textResult.setBounds(14, 180, 473, 98);
		contentPane.add(textResult);

		JButton btnLevelRoot = new JButton("levelRoot");
		btnLevelRoot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnLevelRootCall();
			}
		});
		btnLevelRoot.setBounds(375, 135, 113, 27);
		contentPane.add(btnLevelRoot);

		JButton btnDepth = new JButton("Depth");
		btnDepth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnDepthCall();
			}
		});
		btnDepth.setBounds(131, 90, 113, 27);
		contentPane.add(btnDepth);

		txtDepth = new JTextField();
		txtDepth.setBounds(253, 91, 234, 24);
		contentPane.add(txtDepth);
		txtDepth.setColumns(10);
	}

	protected void btnDepthCall() {
		if (tree == null) {
			textResult.setText("Please create a tree!");
			return;
		}
		int depth = tree.getDepth(tree.getRoot());
		txtDepth.setText(depth + "");
	}

	protected void btnLevelRootCall() {
		if (tree == null) {
			textResult.setText("Please create a tree!");
			return;
		}
		String str = tree.levelTraverse();
		textResult.setText(str);
	}

	protected void btnPostRootCall() {
		if (tree == null) {
			textResult.setText("Please create a tree!");
			return;
		}
		String str = tree.postRootTraverse();
		textResult.setText(str);
	}

	protected void btnInRootCall() {
		if (tree == null) {
			textResult.setText("Please create a tree!");
			return;
		}
		String str = tree.inRootTraverse();
		textResult.setText(str);
	}

	protected void btnPreRootCall() {
		if (tree == null) {
			textResult.setText("Please create a tree!");
			return;
		}
		String str = tree.preRootTraverse();
		textResult.setText(str);
	}

	protected void btnCreateCall() {
		String preOrderStr = txtPreOrder.getText();
		String[] preOrder = preOrderStr.split("");
		String inOrderStr = txtInOrder.getText();
		String[] inOrder = inOrderStr.split("");
		tree = new BiTree(preOrder, inOrder, 0, 0, preOrder.length);
		textResult.setText("Create the tree, choose an traverse methond.");
	}
}
